#include "bsp_timer.h"
//
static uint16_t PrescalerValue = 0;
/**
  * @brief  Configure the TIM IRQ Handler.
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	/* TIM6 clock enable */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);

	/* -----------------------------------------------------------------------
	In this example TIM6 input clock (TIM6CLK) is set to APB1 clock (PCLK1),  
	=> TIM3CLK = PCLK1 = SystemCoreClock = 48 MHz

	To get TIM6 counter clock at 6 MHz, the prescaler is computed as follows:
	Prescaler = (TIM6CLK / TIM6 counter clock) - 1
	Prescaler = (PCLK1 /6 MHz) - 1

	Note: 
	SystemCoreClock variable holds HCLK frequency and is defined in system_FT32f0xx.c file.
	Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
	function to update SystemCoreClock variable value. Otherwise, any configuration
	based on this variable will be incorrect.    
	----------------------------------------------------------------------- */   

	/* Compute the prescaler value */
	PrescalerValue = (uint16_t) (SystemCoreClock  / 6000000) - 1;

	/* Time base configuration */
	TIM_TimeBaseStructure.TIM_Period = 5999;	//65535;
	TIM_TimeBaseStructure.TIM_Prescaler = 0;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);

	/* Prescaler configuration */
	TIM_PrescalerConfig(TIM6, PrescalerValue, TIM_PSCReloadMode_Immediate);	//TIM_PSCReloadMode_Update	TIM_PSCReloadMode_Immediate

	/* Enable the TIM6 gloabal Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = TIM6_DAC_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);   
	
	/* TIM Interrupts enable */
	TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);

	/* TIM6 enable counter */
	TIM_Cmd(TIM6, ENABLE);
}
/**
  * @}
  */
//=======================================================
static int _get(unsigned char *buffer,int len)
{
	
	return 0;
}
//
static int _set(unsigned char *buffer,int len)
{
	
	return 0;
}
//
static int _init(void)
{
	TIM_Config();
	return 0;
}
//
static int _status(unsigned char *buffer,int len)
{

	return 0;
}
//
static int _read(unsigned char *buffer,int len)
{
	
	return 0;
}
//
static int _write(unsigned char *buffer,int len)
{
	
	return 0;
}
//
static void _handler(void *event)
{
	
}
//
const struct driver_interface_t timer_interface =
{
	_get,_set,_init,_status,_read,_write,_handler,
};

